package craw;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.Comment;
import com.restfb.types.Post;

public class CrawPost {

	public static String samsungMobileDir = "samsung_mobile_fb/";
	public static String lastUrl = "fb_last_url.txt";

	public static void main(String[] args) throws IOException {

		String MY_ACCESS_TOKEN = "CAACEdEose0cBAMKi5BTqy8ckL91abvl5gsoyZANaasjaNIFqrIbJN75rx3qfRkXjxXcz0BYjq69B5bnb7OxZAil5zw4PYLoKXhRhwFyZCPURPNRrZBliqi69dDxb11lhxE9AQHY9ZBjkIb9I0mjuC0p7ZCl6ZC90aCLqrbP40DEbcOVomqTgxLjdMjgekXSaSmrJz14yIzviwZDZD";
		FacebookClient facebookClient = new DefaultFacebookClient(
				MY_ACCESS_TOKEN);

		Connection<Post> pagePosts = facebookClient.fetchConnection(
				"anh.quynh9127/feed", Post.class);
		File lasUrlF = new File(lastUrl);
		if (lasUrlF.exists()) {
			String last = FileUtils.readFileToString(lasUrlF);
			pagePosts = facebookClient.fetchConnection(last, Post.class);
		}
		while (true) {
			for (List<Post> posts : pagePosts) {
				for (Post post : posts) {

					// for(Comment comment: post.getComments().getData()){
					// get number of likes of comment
					// }

					String id = post.getId();

					if (id.equals("322004096814_10151909124906815")) {
						System.out.println("lll");
					}

					if (!new File(samsungMobileDir + id).exists()) {
						String message = post.getMessage();
						if (message != null) {
							message = message.replace('\n', ' ');
						}
						long timestamp = post.getCreatedTime().getTime() / 1000;
						// store info
						List<String> commentList = getCommentFromPost(
								facebookClient, id);

						PostStructure ps = new PostStructure(id, message,
								timestamp, commentList);

						writePostStructure(ps);

						System.out.println("last timestamp: " + timestamp);
					}
				}
			}
			try {
				if (pagePosts.getNextPageUrl() != null) {

					FileUtils.write(lasUrlF, pagePosts.getNextPageUrl());
					pagePosts = facebookClient.fetchConnection(
							pagePosts.getNextPageUrl(), Post.class);
				} else {
					break;
				}
			} catch (Exception e) {
				System.out.println(pagePosts.toString());
				break;
			}

		}
	}

	public static void writePostStructure(PostStructure ps) throws IOException {
		List<String> writeList = new ArrayList<String>();
		writeList.add(".message");
		writeList.add(ps.message);
		writeList.add(".timestamp");
		writeList.add(String.valueOf(ps.timestamp));
		writeList.add(".comment");
		for (String s : ps.commentList) {
			// writeList.add(".cdetail");
			writeList.add(s);
		}

		// FileUtils.writeLines(new File(samsungMobileDir+ps.id), writeList);
		FileUtils.writeLines(new File(samsungMobileDir + ps.id), "UTF-8",
				writeList);
	}

	private static List<String> getCommentFromPost(FacebookClient client,
			String post_id) {
		List<String> comments = new ArrayList<String>();

		Connection<Comment> allComments = client.fetchConnection(post_id
				+ "/comments", Comment.class);
		for (List<Comment> postcomments : allComments) {
			for (Comment comment : postcomments) {
				// long likes =
				// comment.getLikeCount()==null?(comment.getLikes()==null?0:comment.getLikes()):comment.getLikeCount();
				comments.add(comment.getMessage().replace('\n', ' '));// +" - "+likes);
			}
		}

		return comments;
	}
}
